<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">

      <el-form-item label="任务名称" prop="taskName">
        <el-input v-model="queryParams.taskName" placeholder="请输入任务名称" clearable size="small" @keyup.enter.native="handleQuery" />
      </el-form-item>

      <el-form-item label="群体" prop="ifGroup">
        <el-select v-model="queryParams.ifGroup" placeholder="请选择营销任务类型" clearable size="small">
          <el-option v-for="dict in ifGroupOptions" :key="dict.dictValue" :label="dict.dictLabel" :value="dict.dictValue" />
        </el-select>
      </el-form-item>
      <el-form-item label="管户机构" prop="orgId">
        <el-select v-model="queryParams.orgId" @change="orgIdBlur(queryParams.orgId)" clearable placeholder="请选择"><el-option v-for="item in orgList" :key="item.value" :label="item.orgName" :value="item.orgId"></el-option></el-select>
      </el-form-item>
      <el-form-item label="管户经理" prop="userId">
        <el-select v-model="queryParams.userId" clearable placeholder="请选择"><el-option v-for="item in mangerList" :key="item.value" :label="item.nickName" :value="item.userName"></el-option></el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>

    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd" v-hasPermi="['buss:sqzxt:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport" v-hasPermi="['buss:taskInfo:export']">数据导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="taskInfoList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" align="center" prop="taskId" :show-overflow-tooltip="true"/>
      <el-table-column label="任务名称" align="center" prop="taskName" />
      <el-table-column label="目标客户类型" align="center" prop="ifGroup" :formatter="ifGroupFormat" />
      <el-table-column label="客户经理" align="center" prop="userId" :formatter="userIdFormat" />
      <el-table-column label="机构ID" align="center" prop="orgId" :formatter="orgIdFormat" />
      <el-table-column label="任务开始时间" align="center" prop="beginDate" />
      <el-table-column label="任务结束时间" align="center" prop="endDate" />
      <el-table-column label="备注" align="center" prop="remark" :show-overflow-tooltip="true"/>
      <el-table-column label="添加时间" align="center" prop="createTime" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <!-- <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)" v-hasPermi="['buss:taskInfo:edit']">修改</el-button> -->
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)" v-hasPermi="['buss:taskInfo:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改营销任务对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="addTaskZH" :model="addTaskZH" :rules="rules" label-width="120px">
        <el-form-item label="任务名称" prop="taskName">
          <el-input v-model="addTaskZH.taskName" placeholder="请输入任务名称" clearable :style="{ width: '100%' }"></el-input>
        </el-form-item>

        <el-form-item label="营销目标" prop="taskTarget">
          <el-select v-model="taskTargetvalue" clearable placeholder="请选择"><el-option v-for="item in taskTargetList" :key="item.value" :label="item.label" :value="item.value"></el-option></el-select>
        </el-form-item>

        <el-form-item label="营销机构" prop="orgId">
          <el-select v-model="orgvalue" @change="orgIdBlur(orgvalue)" clearable placeholder="请选择"><el-option v-for="item in orgList" :key="item.value" :label="item.orgName" :value="item.orgId"></el-option></el-select>
        </el-form-item>

        <el-form-item label="客户经理" prop="userId">
          <el-select v-model="managervalue" clearable placeholder="请选择"><el-option v-for="item in mangerList" :key="item.value" :label="item.nickName" :value="item.userId"></el-option></el-select>
        </el-form-item>

        <el-form-item label="群体" prop="qt">
          <el-radio-group v-model="qt" @change="radioqt(qt)" size="small">
            <el-radio v-for="(item, index) in field101Options" :key="index" :label="item.value" :disabled="item.disabled" border>{{item.label}}</el-radio>
          </el-radio-group>
        </el-form-item>

        <el-form-item label="营销方案" prop="planName"  v-show="isShowT">
          <el-select v-model="planNamevalue" clearable placeholder="请选择"><el-option v-for="item in planNameList" :key="item.planCode" :label="item.planName" :value="item.planCode"></el-option></el-select>
        </el-form-item>

        <el-form-item label="选择客户" prop="" v-show="isShow">
          <el-input v-model="addTaskZH.selectName" placeholder="请输入任务名称" clearable :style="{ width: '70%' }"></el-input>
           <el-button type="primary"  @click="addCustmer"> 新增 </el-button>
        </el-form-item>

        <el-form-item label="任务开始时间" prop="beginDate">
          <el-date-picker
              @change="change"
                v-model="beginDate"
                value-format="yyyy-MM-dd"
                type="date"
                placeholder="选择日期">
              </el-date-picker>
        </el-form-item>

        <el-form-item label="任务结束时间" prop="endDate">
          <el-date-picker
              @change="change"
                v-model="endDate"
                value-format="yyyy-MM-dd"
                type="date"
                placeholder="选择日期">
              </el-date-picker>
        </el-form-item>

        <el-form-item label="备注" prop="remark">
          <el-input v-model="addTaskZH.remark" placeholder="请输入备注" clearable :style="{ width: '100%' }"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>


    <!-- 选择客户 -->
    <el-dialog :title="title" :visible.sync="selectOpen" width="1200px" append-to-body>

      <el-form :model="queryParamsCustmer" ref="queryParamsCustmer" :inline="true" v-show="showSearch" label-width="68px">

        <el-form-item label="客户姓名" prop="name">
          <el-input v-model="queryParamsCustmer.name" placeholder="请输入客户姓名" clearable size="small" @keyup.enter.native="handleQuery" />
        </el-form-item>
        <el-form-item label="证件号码" prop="idCard">
          <el-input v-model="queryParamsCustmer.idCard" placeholder="请输入证件号码" clearable size="small" @keyup.enter.native="handleQuery" />
        </el-form-item>

        <el-form-item label="网格" prop="wg">
          <el-select v-model="wgvalue" placeholder="请选择" clearable size="small">
            <el-option v-for="dict in wgList" :key="dict.value" :label="dict.label" :value="dict.value" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="selecHhandleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>

      </el-form>

      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="selectHandleAdd" v-hasPermi="['buss:sqzxt:add']">确认</el-button>
        </el-col>
      </el-row>

      <el-table v-loading="selectLoading" :data="selectCustmerList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="姓名" align="center" prop="name" />
        <el-table-column label="证件号码" align="center" prop="idCard" />
        <el-table-column label="联系号码" align="center" prop="phone"/>
        <el-table-column label="归属机构" align="center" prop="userId" :formatter="orgIdFormat" />
        <el-table-column label="客户经理" align="center" prop="orgId" :formatter="userIdFormat" />
      </el-table>

    </el-dialog>
  </div>
</template>

<script>
  import {
    listTaskInfo,
    getTaskInfo,
    delTaskInfo,
    addTaskInfo,
    updateTaskInfo,
    orglist,
    queryManager,
    queryPlanNew,
    insert,
    queryCustomerList,
    gridPeasant,
    queryGridSqJm
  } from "@/api/buss/taskInfo";

  export default {
    name: "TaskInfo",
    components: {},
    data() {
      return {
        // 遮罩层
        loading: true,
        selectLoading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        isShow: false,
        isShowT: false,
        wg: '',
        // 营销任务表格数据
        taskInfoList: [],
        selectCustmerList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        selectOpen: false,
        beginDate: '',
        endDate: '',
        qt: '',
        field101Options: [{
          "label": "营销群体",
          "value": '0'
        }, {
          "label": "选择客户",
          "value": '1'
        }],
        planNameList: [],
        planNamevalue: '',
        managervalue: '',
        orgvalue: '',
        orgList: [],
        manger: {
          orgId: ''
        },
        mangerList: [],
        addTaskZH: {
          taskName: '',
          taskId: '',
          beginDate: '',
          endDate: '',
          status: '',
          ifGroup: '',
          taskType: '',
          remark: '',
          type: '',
          groupCode: '',
          customList: '',
          userId: '',
          orgId: '',
          taskTarget: '',
          selectName: ''
        },
        wgList: [
          {
            value: '1',
            label: '存量客户'
          },
          {
            value: '2',
            label: '网格村居'
          },
          {
            value: '3',
            label: '网格社区'
          }
        ],
        wgvalue: '',
        taskTargetList: [
          {
            value: 'CK',
            label: '存款'
          },
          {
            value: 'DK',
            label: '贷款'
          },
          {
            value: 'ETC',
            label: 'ETC'
          },
          {
            value: 'XYK',
            label: '信用卡'
          },
          {
            value: 'SJYH',
            label: '手机银行'
          },
          {
            value: 'WY',
            label: '网银'
          },
          {
            value: 'DXQY',
            label: '短信签约'
          },
          {
            value: 'SDDJ',
            label: '水电代缴'
          },
          {
            value: 'SYB',
            label: '收银宝'
          },
          {
            value: 'POS',
            label: 'POS'
          }
        ],
        taskTargetvalue: '',
        // ID字典
        taskIdOptions: [],
        // 任务名称字典
        taskNameOptions: [],
        // 营销任务类型(0总行，1个人)字典
        taskTypeOptions: [],
        // 目标客户类型字典
        ifGroupOptions: [],
        // 任务开始时间字典
        beginDateOptions: [],
        // 任务结束时间字典
        endDateOptions: [],
        // 已营销客户数字典
        finishNumOptions: [],
        // 客户经理字典
        userIdOptions: [],
        // 添加时间字典
        createTimeOptions: [],
        // 修改时间字典
        updateTimeOptions: [],
        // 添加人字典
        createUserOptions: [],
        // 操作员字典
        updateUserOptions: [],
        // 状态  0不正常  1正常字典
        statusOptions: [],
        // 备注字典
        remarkOptions: [],
        // 营销目标字典
        taskTargetOptions: [],
        // 机构ID字典
        orgIdOptions: [],
        // 营销状态  0未完成  1已完成字典
        completeOptions: [],
        // 任务来源0普通1是首页(首页列如生日提醒,理财到期)字典
        taskSytypeOptions: [],
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          taskName: null,
          taskType: 1,
          ifGroup: null,
          beginDate: null,
          endDate: null,
          finishNum: null,
          userId: '',
          createTime: null,
          updateTime: null,
          createUser: null,
          updateUser: null,
          status: null,
          remark: null,
          taskTarget: null,
          orgId: '',
          complete: null,
          taskSytype: null
        },
        queryParamsCustmer: {
          pageNum: 1,
          pageSize: 10,
          taskName: null,
          taskType: 1,
          name: '',
          idCard: '',
          ifGroup: null,
          beginDate: null,
          endDate: null,
          finishNum: null,
          userId: null,
          createTime: null,
          updateTime: null,
          createUser: null,
          updateUser: null,
          status: null,
          remark: null,
          taskTarget: null,
          orgId: null,
          complete: null,
          taskSytype: null
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {},
        csutmerName: [],
        csutmerList: []
      };
    },
    created() {
      this.getList();
      
      this.getDicts("ifGroup").then(response => {
        this.ifGroupOptions = response.data;
      });
      
      this.getDicts("sys_user_id").then(response => {
        this.userIdOptions = response.data;
      });
      
      this.getDicts("sys_org_id").then(response => {
        this.orgIdOptions = response.data;
      });
      
    },
    methods: {
      /** 查询营销任务列表 */
      getList() {
        this.loading = true;
        listTaskInfo(this.queryParams).then(response => {
          this.taskInfoList = response.rows;
          this.total = response.total;
          this.loading = false;
        });

        //下拉机构
        orglist().then(response => {
          this.orgList = response.rows;
        });
        queryPlanNew().then(response => {
          this.planNameList = response.rows;
        });
      },
      radioqt(qt) {
        //console.log(qt)
        if(qt == '1'){
          this.isShow = true;
          this.isShowT = false;
        }else{
          this.isShow = false;
          this.isShowT = true;
        }
      },
      addCustmer() {
        console.log(1111)
        this.selectOpen = true;
        this.selectLoading = true;
        this.queryCustmer();
      },
      queryCustmer() {
        queryCustomerList(this.queryParamsCustmer).then(response => {
          this.selectCustmerList = response.rows;
          this.total = response.total;
          this.selectLoading = false;
        });
      },
      gridPeasantList() {
        gridPeasant(this.queryParamsCustmer).then(response => {
          this.selectCustmerList = response.rows;
          this.total = response.total;
          this.selectLoading = false;
        });
      },
      queryGridSqJmList() {
        queryGridSqJm(this.queryParamsCustmer).then(response => {
          this.selectCustmerList = response.rows;
          this.total = response.total;
          this.selectLoading = false;
        });
      },
      change() {
        this.addTaskZH.beginDate = this.beginDate;
        this.addTaskZH.endDate = this.endDate;
          //console.log(bj);
        },
      //客户经理
      orgIdBlur(mg) {
        this.manger.orgId = mg;
        queryManager(this.manger).then(response => {
          this.mangerList = response.rows;
        });
      },
      // ID字典翻译
      taskIdFormat(row, column) {
        return this.selectDictLabel(this.taskIdOptions, row.taskId);
      },
      // 任务名称字典翻译
      taskNameFormat(row, column) {
        return this.selectDictLabel(this.taskNameOptions, row.taskName);
      },
      // 营销任务类型(0总行，1个人)字典翻译
      taskTypeFormat(row, column) {
        return this.selectDictLabel(this.taskTypeOptions, row.taskType);
      },
      // 目标客户类型字典翻译
      ifGroupFormat(row, column) {
        return this.selectDictLabel(this.ifGroupOptions, row.ifGroup);
      },
      // 任务开始时间字典翻译
      beginDateFormat(row, column) {
        return this.selectDictLabel(this.beginDateOptions, row.beginDate);
      },
      // 任务结束时间字典翻译
      endDateFormat(row, column) {
        return this.selectDictLabel(this.endDateOptions, row.endDate);
      },
      // 已营销客户数字典翻译
      finishNumFormat(row, column) {
        return this.selectDictLabel(this.finishNumOptions, row.finishNum);
      },
      // 客户经理字典翻译
      userIdFormat(row, column) {
        return this.selectDictLabel(this.userIdOptions, row.userId);
      },
      // 添加时间字典翻译
      createTimeFormat(row, column) {
        return this.selectDictLabel(this.createTimeOptions, row.createTime);
      },
      // 修改时间字典翻译
      updateTimeFormat(row, column) {
        return this.selectDictLabel(this.updateTimeOptions, row.updateTime);
      },
      // 添加人字典翻译
      createUserFormat(row, column) {
        return this.selectDictLabel(this.createUserOptions, row.createUser);
      },
      // 操作员字典翻译
      updateUserFormat(row, column) {
        return this.selectDictLabel(this.updateUserOptions, row.updateUser);
      },
      // 状态  0不正常  1正常字典翻译
      statusFormat(row, column) {
        return this.selectDictLabel(this.statusOptions, row.status);
      },
      // 备注字典翻译
      remarkFormat(row, column) {
        return this.selectDictLabel(this.remarkOptions, row.remark);
      },
      // 营销目标字典翻译
      taskTargetFormat(row, column) {
        return this.selectDictLabel(this.taskTargetOptions, row.taskTarget);
      },
      // 机构ID字典翻译
      orgIdFormat(row, column) {
        return this.selectDictLabel(this.orgIdOptions, row.orgId);
      },
      // 营销状态  0未完成  1已完成字典翻译
      completeFormat(row, column) {
        return this.selectDictLabel(this.completeOptions, row.complete);
      },
      // 任务来源0普通1是首页(首页列如生日提醒,理财到期)字典翻译
      taskSytypeFormat(row, column) {
        return this.selectDictLabel(this.taskSytypeOptions, row.taskSytype);
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          taskId: null,
          taskName: null,
          taskType: null,
          ifGroup: null,
          beginDate: null,
          endDate: null,
          finishNum: null,
          userId: null,
          createTime: null,
          updateTime: null,
          createUser: null,
          updateUser: null,
          status: "0",
          remark: null,
          taskTarget: null,
          orgId: null,
          complete: null,
          taskSytype: null
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      selecHhandleQuery() {
        console.log(this.wgvalue)
        if(this.wgvalue == '1'){
          this.queryParamsCustmer.pageNum = 1;
          this.queryCustmer();
        }else if(this.wgvalue == '2'){
          this.gridPeasantList();
        }else if(this.wgvalue == '3'){
          this.queryGridSqJmList();
        }

      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.taskId)
        this.single = selection.length !== 1
        this.multiple = !selection.length
        this.csutmerName = selection;
      },
      /** 新增按钮操作 */
      selectHandleAdd() {
        for (var i = 0; i < this.csutmerName.length; i++) {
          this.csutmerList.push(this.csutmerName[i].name)
          console.log(this.csutmerName[i].name)
        }
        this.addTaskZH.selectName = this.csutmerList.toString();
        this.selectOpen = false;
        console.log(this.csutmerList.toString())
        console.log(JSON.stringify(this.csutmerList))
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加营销任务";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const taskId = row.taskId || this.ids
        getTaskInfo(taskId).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "修改营销任务";
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["addTaskZH"].validate(valid => {
          if (valid) {
            if (this.form.taskId != null) {
              updateTaskInfo(this.form).then(response => {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              console.log(JSON.stringify(this.csutmerList))
              var addTaskInfoZH = {
                taskName: this.addTaskZH.taskName,
                beginDateStr: this.addTaskZH.beginDate,
                endDateStr: this.addTaskZH.endDate,
                ifGroup: this.qt,
                taskType: '1',
                remark: this.addTaskZH.remark,
                type: '',
                groupCode: this.planNamevalue,
                customList: this.csutmerList,
                userId: this.managervalue,
                orgId: this.orgvalue,
                taskTarget: this.taskTargetvalue
              }
              console.log(addTaskInfoZH)
              insert(addTaskInfoZH).then(response => {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const taskIds = row.taskId || this.ids;
        this.$confirm('是否确认删除营销任务编号为"' + taskIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delTaskInfo(taskIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
      },
      /** 导出按钮操作 */
      handleExport() {
        this.queryParams.idCard=null
        this.download('buss/taskInfo/exportZh', {
          ...this.queryParams
        }, `支行任务导出.xlsx`)
      }
    }
  };
</script>
